// =================================================================================
// THIS IS A TEMPLATE FILE
// 
// To set these up to for a new state, do a find/replace on the constant STATE_NAME
// 
// Also replace the #define just below to a more appropriate name manually.
// =================================================================================

#include "STATE_NAME.hpp"

using namespace Ogre;

//=============================
// CONSTRUCTOR
//=============================
STATE_NAME::STATE_NAME()
{

}

//=============================
// START
//=============================

// Basic setup of the scene and state.
void STATE_NAME::enter()
{
	// Log the action of changing states.
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Entering STATE_NAME...");
 
	// Create scene manager.
    m_pSceneMgr = OgreFramework::getSingletonPtr()->m_pRoot->createSceneManager(ST_GENERIC, "GameSceneMgr");
    m_pSceneMgr->setAmbientLight(Ogre::ColourValue(0.1f, 0.1f, 0.1f));

	// Setup the camera.
    m_pCamera = m_pSceneMgr->createCamera("STATE_NAMECamera");
    m_pCamera->setPosition(Vector3(30, 0, 75));
	m_pCamera->lookAt(Vector3(0, 0, 0));
    m_pCamera->setNearClipDistance(1);

	// Set camera aspect ratio.
    m_pCamera->setAspectRatio(Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualWidth()) /
        Real(OgreFramework::getSingletonPtr()->m_pViewport->getActualHeight()));

	// Set our camera to the viewport.
	OgreFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);

	// Create the Scene.
	createScene();
}

// Fills the scene with the generic required stuff.
void STATE_NAME::createScene()
{
	// Create a light.
	m_pSceneMgr->createLight("Light")->setPosition(75, 75, 75);

	//// Instantiate the DotSceneLoader.
	//DotSceneLoader* pDotSceneLoader = new DotSceneLoader();

	//// Load the DotScene into our Scene.
	//pDotSceneLoader->parseDotScene("GenericSpace.scene", "General", m_pSceneMgr, m_pSceneMgr->getRootSceneNode());
	//delete pDotSceneLoader;
}

//=============================
// UPDATE CYCLE
//=============================

// Update the whole state.
void STATE_NAME::update(double timeSinceLastFrame)
{
	// Leave and remove from the active state stack if requested.
	if (m_bQuit == true)
	{
		popAppState();
		return;
	}
}

//=============================
// INPUT
//=============================

// By default, all these functions should pass the input events back to the Framework.
bool STATE_NAME::keyPressed(const OIS::KeyEvent& keyEventRef)
{
	// More often than not, something needs to be done when the user presses Escape.
	// Handle that here.
	if (OgreFramework::getSingletonPtr()->m_pKeyboard->isKeyDown(OIS::KC_ESCAPE))
	{
		m_bQuit = true;
		return true;
	}

	OgreFramework::getSingletonPtr()->keyPressed(keyEventRef);
	return true;
}

// By default, all these functions should pass the input events back to the Framework.
bool STATE_NAME::keyReleased(const OIS::KeyEvent &keyEventRef)
{
    OgreFramework::getSingletonPtr()->keyPressed(keyEventRef);
    return true;
}

// By default, all these functions should pass the input events back to the Framework.
bool STATE_NAME::mouseMoved(const OIS::MouseEvent &evt)
{
	OgreFramework::getSingletonPtr()->mouseMoved(evt);
    return true;
}

// By default, all these functions should pass the input events back to the Framework.
bool STATE_NAME::mousePressed(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
	OgreFramework::getSingletonPtr()->mouseMoved(evt);
    return true;
}

// By default, all these functions should pass the input events back to the Framework.
bool STATE_NAME::mouseReleased(const OIS::MouseEvent &evt, OIS::MouseButtonID id)
{
	OgreFramework::getSingletonPtr()->mouseMoved(evt);
    return true;
}

//=============================
// PAUSE
//=============================

// Called by the app state manager when the game state is to be paused.
bool STATE_NAME::pause()
{
    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Pausing STATE_NAME..."); 
    return true;
}

//=============================
// RESUME
//=============================

// Called by the AppStateManager when the game state is to be resumed. Also sets
// the camera to actively use the Viewport.
void STATE_NAME::resume()
{
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Resuming STATE_NAME...");
	OgreFramework::getSingletonPtr()->m_pViewport->setCamera(m_pCamera);
	m_bQuit = false;
}


//=============================
// DESTRUCTOR
//=============================

// Similar to the pause function, but it also destroys the Camera, SceneManager
// and the RaySceneQuery.
void STATE_NAME::exit()
{
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Leaving STATE_NAME...");

	m_pSceneMgr->destroyCamera(m_pCamera);

	if (m_pSceneMgr)
		OgreFramework::getSingletonPtr()->m_pRoot->destroySceneManager(m_pSceneMgr);
}